<script setup>
import { ref,onMounted } from 'vue'
import { getAllUser } from '../../api/user'
import { queryOutboundOrderWeights,updateOutboundOrderWeights } from '../../api/outboundorderweights'
import { ElMessage } from 'element-plus'

//定义userlist集合
const userList = ref([])
const getUserList = async () => { 
  const res = await getAllUser()
  userList.value = res.data
}

//分页条件查询
const query = async () =>{
  const result = await queryOutboundOrderWeights(page.value,pageSize.value,outBoundOrderWeightFrom.value.status,outBoundOrderWeightFrom.value.weightsCode)
  outBoundOrderWeightList.value = result.data.rows
  total.value = result.data.total
}
//清空表单数据
const clear = () => {
  outBoundOrderWeightFrom.value = { status: '', weightsCode: ''}
  query()
}
//定义表格数据
const outBoundOrderWeightList = ref([])
//定义表单数据
const outBoundOrderWeightFrom = ref({ status: '', weightsCode: ''})

//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({weightsEmpId:'',weightsTime:'',weightVal:'',weightsUnit:''})

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}

//挂载
onMounted(() => { 
  query()
  getUserList()
})

//定义确定打包的点击事件
const confirm = (id) => { 
  title.value = '打包'
  dialogShow.value = true
  info.value.id = id
  //清空表单数据
  dialogData.value = {weightsEmpId:'',weightsTime:'',weightVal:'',weightsUnit:''}
}

//定义接收数据
const info = ref({id:'',weightsEmpId:'',weightsTime:'',weightVal:'',weightsUnit:''})

//确认称重信息的点击事件
const confirmWeightsInfo = async ()=>{ 
  info.value.weightsEmpId = dialogData.value.weightsEmpId
  info.value.weightsTime = dialogData.value.weightsTime
  info.value.weightVal = dialogData.value.weightVal
  info.value.weightsUnit = dialogData.value.weightsUnit
  const result = await updateOutboundOrderWeights(info.value)
  if(result.code){
    ElMessage.success('操作成功')
    query()
    dialogShow.value = false
    //清空表单数据
    dialogData.value = {weightsEmpId:'',weightsTime:'',weightVal:'',weightsUnit:''}
    //重置变量里面的数据
    info.value = {id:'',weightsEmpId:'',weightsTime:'',weightVal:'',weightsUnit:''}
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}
</script>
<template>
<div><h3>称重</h3></div>

<div>
  <el-form :inline="true" :model="outBoundOrderWeightFrom" class="demo-form-inline">
    <el-form-item>
      <el-select v-model="outBoundOrderWeightFrom.status" placeholder="请选择状态" style="width: 200px;">
        <el-option label="未称重" value="1"></el-option>
        <el-option label="已称重" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="outBoundOrderWeightFrom.weightsCode" placeholder="称重单号" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="outBoundOrderWeightList" border style="width: 100%" >
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="weightsCode" label="称重单号" align="center"  width="200"></el-table-column>
    <el-table-column prop="skuCode" label="商品编码" align="center"  width="200"></el-table-column>
    <el-table-column prop="skuTitle" label="商品名称" align="center"  width="200"></el-table-column>
    <el-table-column prop="skuWeight" label="商品重量" align="center"  width="200"></el-table-column>
    <el-table-column prop="status" label="打包状态" align="center" width="120">
      <template #default="scope">
        <el-tag v-if="scope.row.status == 1" type="info">未称重</el-tag>
        <el-tag v-else="scope.row.status == 2" type="success">已称重</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="count" label="订单数量" align="center" width="150"/>
    <el-table-column prop="weightVal" label="称重重量" align="center" width="150"/>
    <el-table-column prop="weightsUnit" label="称重单位" align="center" width="150">
      <template #default="scope">
        <span v-if="scope.row.weightsUnit == 1">g</span>
        <span v-else-if="scope.row.weightsUnit == 2">kg</span>
        <span v-else-if="scope.row.weightsUnit == 3">t</span>
        <span v-else>{{ scope.row.weightsUnit }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="weightsTime" label="称重时间" align="center" width="150"/>
    <el-table-column prop="weightsEmpId" label="称重员工" align="center" width="150">
      <template #default="scope">
        <span>{{ userList.find(user => user.id == scope.row.weightsEmpId)?.name || '未拣货'}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" align="center" width="200"/>
    <el-table-column prop="createBy" label="创建人" align="center" width="150"/>
    <el-table-column fixed="right" label="操作" align="center" width="80">
      <template #default="scope">
        <el-button type="primary" size="small" @click="confirm(scope.row.id)" plain><el-icon><Select /></el-icon></el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="500" align-center>
    <el-form :model="dialogData">
      <el-form-item>
        <el-select v-model="dialogData.weightsEmpId" placeholder="请称重打包员" style="width: 350px;">
          <el-option v-for="user in userList" :label="user.name" :value="user.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dialogData.weightsTime" type="date" placeholder="称重时间" value-format="YYYY-MM-DD" style="width: 350px;" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="dialogData.weightVal" placeholder="请输入称重重" style="width: 350px;"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dialogData.weightsUnit" placeholder="请选择称重单位" style="width: 350px;">
          <el-option label="g" value="1"></el-option>
          <el-option label="kg" value="2"></el-option>
          <el-option label="t" value="3"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default">取消</el-button>
      <el-button type="primary" @click="confirmWeightsInfo">确定</el-button>
    </template>
  </el-dialog>
</div>
</template>
<style scoped>
</style>